﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao.CommonDao;
using CSP.Model.CSP;
using CSP.DataAccess;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class AuthenticationDao : CSPCommonDao, IAuthenticationDao
    {

        public AuthenticationDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        /// <summary>
        /// 获取所有的对象
        /// 范围：
        /// </summary>
        /// <returns></returns>
        public ICollection<Model.CSP.Authentication> FindAll()
        {
            return _context.Authentications.ToArray();
        }

        /// <summary>
        /// 根据Id 获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.CSP.Authentication GetDataById(int id)
        {
            return _context.Authentications.FirstOrDefault(a => a.Id == id);
        }

        /// <summary>
        /// 根据Ids获取 数据
        /// 范围：
        /// </summary>
        /// <returns></returns>
        public ICollection<Model.CSP.Authentication> GeDatatByIds(int[] Ids)
        {
            var result = _context.Authentications.Where(a => Ids.Contains(a.Id));
            if (result.Count() == 0)
            {
                return null;
            }

            return result.ToArray();
        }

        /// <summary>
        /// 根据用户名和主键ID 获取单条信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public Authentication GetByIdAndUser(int id, string userName)
        {
            return _context.Authentications.FirstOrDefault(a => a.Id == id && a.Username == userName);
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="T"></param>
        public void Add(Model.CSP.Authentication obj)
        {
            _context.Authentications.Add(obj);
            _context.SaveChanges();
        }

        /// <summary>
        /// 通过对象 更新对象
        /// </summary>
        public void Update(Model.CSP.Authentication obj)
        {
            Authentication authentication = GetDataById(obj.Id);
            if (authentication != null)
            {
                authentication = obj;
            }
            _context.SaveChanges();
        }

        /// <summary>
        /// 根据Id删除 数据
        /// 范围：
        /// </summary>
        /// <param name="id"></param>
        public Authentication DeleteById(int id)
        {
            Authentication authentication = GetDataById(id);
            if (authentication == null)
            {
                return authentication;
            }
            _context.Authentications.Remove(authentication);
            _context.SaveChanges();
            return authentication;
        }

        public ICollection<Authentication> GetByUser(string userName)
        {
            var result = _context.Authentications.Where(a => a.Username == userName);
            if (result.Count() == 0)
            {
                return null;
            }
            return result.ToArray();

        }



        public Authentication RemoveByUserAndCertificateType(string userName, int certificateType)
        {
            Authentication authentication = _context.Authentications.FirstOrDefault(a => a.Username == userName && a.CertificateType == certificateType);
            if (authentication == null)
            {
                return null;
            }
            _context.Authentications.Remove(authentication);
            _context.SaveChanges();

            return authentication;
        }

        public Authentication RemoveByIdAndUser(int id, string userName)
        {
            Authentication authentication = _context.Authentications.FirstOrDefault(a => a.Username == userName && a.Id == id);
            if (authentication == null)
            {
                return null;
            }
            _context.Authentications.Remove(authentication);
            _context.SaveChanges();

            return authentication;
        }
    }
}
